package com.lixixi.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lixixi.tingshu.album.service.AlbumTrackInfoService;
import com.lixixi.tingshu.common.anno.NeedLogin;
import com.lixixi.tingshu.common.result.Result;
import com.lixixi.tingshu.model.album.TrackInfo;
import com.lixixi.tingshu.query.album.TrackInfoQuery;
import com.lixixi.tingshu.vo.album.AlbumTrackListVo;
import com.lixixi.tingshu.vo.album.TrackInfoVo;
import com.lixixi.tingshu.vo.album.TrackListVo;
import com.lixixi.tingshu.vo.album.TrackStatVo;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/album/trackInfo")
public class AlbumTrackInfoUploadApiController {

	@Autowired
	AlbumTrackInfoService albumTrackInfoService;


	@PostMapping("/uploadTrack")
	@NeedLogin
	Result<JSONObject> uploadTrack(@RequestParam("file") MultipartFile multipartFile) throws IOException {
		JSONObject trackInfo = albumTrackInfoService.uploadTrack(multipartFile);
		return Result.ok(trackInfo);
	}

	@PostMapping("/saveTrackInfo")
	@NeedLogin
	Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
		albumTrackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	@PostMapping("/findUserTrackPage/{page}/{limit}")
	@NeedLogin
	Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable(value = "page", required = true) Long page
			, @PathVariable(value = "limit", required = true) Long limit
			, @PathVariable(value = "TrackInfoQuery", required = false) TrackInfoQuery trackInfoQuery) {
		if (log.isDebugEnabled())
		{
			log.info("查询分页==> page:{}, limit:{}, albumInfoQuery:{}", page, limit, trackInfoQuery);
		}
		IPage<TrackListVo> trackPage = albumTrackInfoService.findUserTrackPage(page, limit
				, trackInfoQuery != null ? trackInfoQuery : new TrackInfoQuery());
		if (log.isDebugEnabled())
		{
			log.info("返回分页:{}", trackPage);
		}
		return Result.ok(trackPage);
	}

	@GetMapping("/getTrackInfo/{trackId}")
	Result<TrackInfo> getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = albumTrackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	@GetMapping("/getAlbumIdByTrackId/{trackId}")
	Result<Long> getAlbumIdByTrackId(@PathVariable("trackId") Long trackId){
		TrackInfo trackInfo = albumTrackInfoService.lambdaQuery()
				.select(TrackInfo::getAlbumId)
				.eq(TrackInfo::getId, trackId).one();
		return Result.ok(trackInfo.getAlbumId());
	}


	@PutMapping("/updateTrackInfo/{trackId}")
	@NeedLogin
	Result updateTrackInfo(@PathVariable("trackId") Long trackId, @RequestBody TrackInfoVo trackInfoVo) throws TencentCloudSDKException {
		albumTrackInfoService.updateTrackInfo(trackId, trackInfoVo);
		return Result.ok();
	}

	@DeleteMapping("/removeTrackInfo/{trackId}")
	@NeedLogin
	Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
		albumTrackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

//	http://127.0.0.1/api/album/trackInfo/findAlbumTrackPage/661/1/10


	@GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
	@NeedLogin(isLogin = false)
	Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId
			, @PathVariable(value = "page", required = true) Long page
			, @PathVariable(value = "limit", required = true) Long limit) {
		if (log.isDebugEnabled())
		{
			log.info("查询专辑:{}下分页==> page:{}, limit:{}", albumId, page, limit);
		}
		IPage<AlbumTrackListVo> albumTrackInfoVoPage = albumTrackInfoService.findAlbumTrackPage(albumId, page, limit);
		if (log.isDebugEnabled())
		{
			log.info("返回分页:{}", albumTrackInfoVoPage);
		}
		return Result.ok(albumTrackInfoVoPage);
	}

	@GetMapping("/getTrackStatVo/{trackId}")
	Result<TrackStatVo> getTrackStatVo(@PathVariable("trackId") Long trackId) {
		TrackStatVo trackStatVo = albumTrackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}

	// http://127.0.0.1/api/album/trackInfo/findUserTrackPaidList/2872
	@GetMapping("/findUserTrackPaidList/{trackId}")
	Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable("trackId") Long trackId){
		return Result.ok(albumTrackInfoService.findUserTrackPaidList(trackId));
	}

	@GetMapping("/getOrderTrackInfoList")
	@NeedLogin(isLogin = true)
	Result<List<TrackInfo>> getOrderTrackInfoList(Long itemId, Integer trackCount){
		return Result.ok(albumTrackInfoService.getOrderTrackInfoList(itemId, trackCount));
	}


}
